local dsp = require "luci.dispatcher"
require("luci.http")
require("uci")
x = uci.cursor()
m = Map("hsion_mLink",
	translate("MultiLink"))

s = m:section(TypedSection, "general", translate("General"))
s.anonymous = true
s.addremove = false

o = s:option(Flag, "enabled", translate("Enabled"))
o.rmempty = false
o.default = false

s = m:section(TypedSection, "interface", translate("Interface"))
s.anonymous = true
s.addremove = true
--s.sortable  = true
s.template = "cbi/tblsection"
s.extedit   = dsp.build_url("admin/network/hsion_mLink/%s")

--function s.create和s.parse作用是：点击新建按钮，直接跳出配置页面
--暂时不深究是如何实现的了
function s.create(self, section)
	--统计数量>8禁止新建
	local count = 0
	x:foreach("hsion_mLink", 
			  "interface",
			  function(s)
				count = count + 1
			end
            )
	
	if count < 8 then
		created = TypedSection.create(self, section)
	else 
		created = nil
	end
end

function s.parse(self, ...)
	TypedSection.parse(self, ...)
	if created then
		m.uci:save("hsion_mLink")
		luci.http.redirect(dsp.build_url("admin/network/hsion_mLink", created))
	end

end

--description
o = s:option(DummyValue, "description", translate("Device Description"))
o.placeholder = translate("Device Description")

--interface
o = s:option(DummyValue, "net", translate("Network"))
o.rawhtml = true
function o.cfgvalue(self, s)
    local net = self.map:get(s, "net")
    if net then
        return "<font color='blue'>" .. net .. "</font>"
    else
        return "<font color='red'>" .. translate("Not Set") .. "</font>"
    end
end

--weight
o = s:option(DummyValue, "weight", translate("Weight"))
o.rawhtml = true
function o.cfgvalue(self, s)
    local weight = self.map:get(s, "weight")
    if weight then
        return "<font color='blue'>" .. weight .. "</font>"
    else
        return "<font color='red'>" .. translate("Not Set") .. "</font>"
    end
end

--track_ip
o = s:option(DummyValue, "track_ip", translate("Track IP"))
o.rawhtml = true
function o.cfgvalue(self, s)
    local track_ip = self.map:get(s, "track_ip")
    if track_ip then
        return "<font color='blue'>" .. track_ip .. "</font>"
    else
        return "<font color='red'>" .. translate("Not Set") .. "</font>"
    end
end

--count
o = s:option(DummyValue, "count", translate("Ping Count Each Track IP"))
o.rawhtml = true
function o.cfgvalue(self, s)
    local value = self.map:get(s, "count")
    if value then
        return "<font color='blue'>" .. value .. "</font>"
    else
        return "<font color='red'>" .. translate("Not Set") .. "</font>"
    end
end

--up
o = s:option(DummyValue, "up", translate("Up"))
o.rawhtml = true
function o.cfgvalue(self, s)
    local value = self.map:get(s, "up")
    if value then
        return "<font color='blue'>" .. value .. "</font>"
    else
        return "<font color='red'>" .. translate("Not Set") .. "</font>"
    end
end

--down

o = s:option(DummyValue, "down", translate("Down"))
o.rawhtml = true
function o.cfgvalue(self, s)
    local value = self.map:get(s, "down")
    if value then
        return "<font color='blue'>" .. value .. "</font>"
    else
        return "<font color='red'>" .. translate("Not Set") .. "</font>"
    end
end

--ping timeout and interval
o = s:option(DummyValue, "other", translate("Other"))
o.rawhtml = true
function o.cfgvalue(self, s)
    local timeout = self.map:get(s, "timeout")
    local interval = self.map:get(s, "interval")
    if timeout and interval then
        return "<font color='blue'>timeout:"  .. timeout  .. "</font>" .. "<br />" .. 
               "<font color='blue'>interval:" .. interval .. "</font>"
    else 
        if timeout then
            return "<font color='blue'>timeout:" .. timeout  .. "</font>" .. "<br />" .. 
                   "<font color='red'>interval:" .. translate("Not Set") .. "</font>"
        elseif interval then
            return "<font color='blue'>timeout:" .. translate("Not Set")  .. "</font>" .. "<br />" .. 
                   "<font color='red'>interval:" .. interval .. "</font>"
        else
            return "<font color='red'>timeout:" .. translate("Not Set")  .. "</font>" .. "<br />" .. 
                   "<font color='red'>interval:" .. translate("Not Set") .. "</font>"
        end
    end
end

local apply = luci.http.formvalue("cbi.apply")
if apply then
    luci.sys.exec("killall -9 hsion_mLink")
	luci.sys.exec("/etc/init.d/hsion_mLink start > /dev/null 2>&1 &")
end

return m
